package com.tao.auto.batchtao.convert;

import com.tao.auto.batchtao.base.ParamContext;
import com.tao.auto.batchtao.database.dao.MgtUserInfoRepository;
import com.tao.auto.batchtao.database.entity.MgtUserInfo;
import com.tao.auto.batchtao.home.HomeLoginBaseFlowImpl;
import com.tao.auto.batchtao.home.account.HomeAccount;
import com.tao.auto.batchtao.home.account.HomeAccountQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class MgtConvert {

    @Autowired
    private MgtUserInfoRepository mgtUserInfoRepository;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HomeLoginBaseFlowImpl homeLoginBaseFlow;

    public void mgtConvert(String phone, long number) {
        int page = 0;
        List<MgtUserInfo> mgtUserInfos;
        try {
            while (!(mgtUserInfos = mgtUserInfoRepository.findAll(PageRequest.of(page++, 100)).getContent()).isEmpty()) {
                mgtUserInfos.stream().filter(mgtUserInfo -> mgtUserInfo.getSign().equals(phone))
                        .forEach(mgtUserInfo -> {
                            try {
                                TimeUnit.SECONDS.sleep(5);
                                HomeAccount homeAccount = new HomeAccount();
                                homeAccount.setAccount(mgtUserInfo.getUsername());
                                homeAccount.setPassword(mgtUserInfo.getPassword());
                                homeAccount.setSpread(mgtUserInfo.getFatherInvitationCode() + "");
                                ParamContext<String> login = homeLoginBaseFlow.login(new ParamContext<HomeAccount>().paramContext(homeAccount));
                                HttpHeaders headers = new HttpHeaders();
                                headers.add("Authori-zation", "Bearer " + login.getData());
                                headers.setContentType(MediaType.APPLICATION_JSON);
                                String url = "http://cr.dongweicom.com/api/extract/userMgtZ";
                                Map<String, String> params = new HashMap<>(8);
                                params.put("cardnum", phone);
                                params.put("money", number == 0 ? mgtUserInfo.getNumber() + "" : number + "");
                                params.put("extract_type", "bank");
                                params.put("bankname", "请选择银行");
                                HttpEntity<Map> entity = new HttpEntity<>(params, headers);
                                ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(url, entity, String.class);
                                System.out.println(stringResponseEntity.getBody());
                                mgtUserInfo.setNumber(0L);
                                mgtUserInfoRepository.save(mgtUserInfo);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        });
            }
        } catch (Exception e) {

        }

    }
}
